package com.humanminoritygame.web;

import java.util.Random;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.humanminoritygame.common.Utils;
import com.humanminoritygame.model.User;
import com.humanminoritygame.model.UserRepo;

@Controller
public class UserController {
  private static final Pattern EMAIL_PATTERN = Pattern.compile("^\\w+([_\\.-]\\w+)*@(\\w+([_\\.-]\\w+)+)");
  private static final Pattern USER_NAME_PATTERN = Pattern.compile("\\w+");
  UserRepo users;
  
  @Autowired
  public UserController(UserRepo users){
    this.users = users;
  }

  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView profile(){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("userProfile");
    return mav;
  }

  @RequestMapping(method = RequestMethod.POST)
  public String savePassword(HttpServletRequest request, HttpSession session, @RequestParam("pass1")String pass1, @RequestParam("pass2")String pass2, @RequestParam("oldpass")String oldpass ){
    boolean error = false;
    String errores = new String();

    User user = (User)session.getAttribute("user");

    if(pass1.equals("") || pass2.equals("") || oldpass.equals("")){
      error = true;
      errores = errores + "Please complete every field<br/>";
    }

    if(pass1.length()>50 || pass2.length()>50){
      error = true;
      errores = errores + "Please enter a valid password<br/>";
    }

    if(!(Utils.encrypt(oldpass)).equals(user.getPassword())){
      error = true;
      errores = errores + "Incorrect password<br/>";
    }

    if(!pass1.equals(pass2)){
      error = true;
      errores = errores + "Password mismatch<br/>";
    }

    if(!error){
      request.setAttribute("modifiedok", "Your password has been modified.");
      User u = users.getUserById(user.getId());
      u.setPassword(Utils.encrypt(pass1));
    } else {
      request.setAttribute("modifiederror", errores);
    }
    return "forward:changePassword2";
  }

  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView changePassword(){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("userChangePassword");
    return mav;
  }

  @RequestMapping(method = RequestMethod.POST)
  public ModelAndView changePassword2(HttpSession session){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("userChangePassword");
    return mav;
  }

  @RequestMapping(method = RequestMethod.POST)
  public String saveMail(HttpServletRequest request, HttpSession session, @RequestParam("email")String email, @RequestParam("email2")String email2) {
    if(!Utils.validatePattern(email, EMAIL_PATTERN) || !Utils.validatePattern(email2, EMAIL_PATTERN) || email.length()>100 || email2.length()>100){
      request.setAttribute("emailbkup", email);
      request.setAttribute("modifiederror", "Please enter valid email addresses");
      return "forward:changeMail2";
    }
    
    if(!email.equals(email2)){
      request.setAttribute("modifiederror", "The emails don't match");
      request.setAttribute("emailbkup", email);
      return "forward:changeMail2";
    }

    User u = users.getUserById(((User)session.getAttribute("user")).getId());
    Random r = new Random();
    u.setMail(email);
    u.setConfirmedMail(Math.abs(r.nextInt()));
    Utils.enviarMail(u, request.getRequestURL().toString());
    request.setAttribute("modifiedok", "Your new email must be confirmed. Please check your inbox.");
    return "forward:changeMail2";
  }

  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView changeMail(){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("userChangeMail");
    return mav;
  }

  @RequestMapping(method = RequestMethod.POST)
  public ModelAndView changeMail2(HttpSession session){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("userChangeMail");
    return mav;
  }

  @RequestMapping(method = RequestMethod.POST)
  public String saveName(HttpServletRequest request, HttpSession session, @RequestParam("name")String name){
    if(!Utils.validatePattern(name, USER_NAME_PATTERN) || name.equals("") || name.length()>50){
      request.setAttribute("modifiederror", "Invalid name");
      request.setAttribute("namebkup", name);
    } else if(users.getUserByName(name)!=null){
      request.setAttribute("modifiederror", "The name you entered has already been taken");
      request.setAttribute("namebkup", name);
    } else {
      User u = users.getUserById(((User)session.getAttribute("user")).getId());
      u.setName(name);
      session.setAttribute("user", u);
      request.setAttribute("modifiedok", "Your username has been successfully updated");
    }
    return "forward:changeName2";
  }

  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView changeName(){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("userChangeName");
    return mav;
  }

  @RequestMapping(method = RequestMethod.POST)
  public ModelAndView changeName2(HttpSession session){
    ModelAndView mav = new ModelAndView();
    mav.setViewName("userChangeName");
    return mav;
  }
}
